using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Robot.Business;
using Robot.Data;
using Robot.Entities;
using Scb.Framework;
using System.IO;
using WebRobot.App_code;
using System.Text;

namespace WebRobot.Pages
{
  public partial class ImportRatePage : BasePage
  {
    static HttpFileCollection hfc;

    #region Event

    protected void Page_Load(object sender, EventArgs e)
    {

      string sId = "";
      sId = Request.QueryString["Id"];
      Guid id = new Guid();
      if (sId != null)
      {
        id = new Guid(sId);
        if (id.ToString() != "")
        {
          try
          {
            tblFileUploadCollection list = new tblFileUploadCollection();
            list.Add(tblFileUploadService.GettblFileUpload(id));
            gvFileUpload.DataSource = list;
            gvFileUpload.DataBind();
            gvFileUpload.Visible = true;

            gvRate.DataSource = tblInternalRateService.GettblInternalRateByFromFileID(id);
            gvRate.DataBind();
            gvRate.Visible = true;
          }
          catch (Exception ex)
          {
            Logger.Error("WebRobot.Pages.ImportRatePage::Page_Load", ex);
          }
        }
      }
      if (dateTo.Value == null)
      {
        dateTo.Value = DateTime.Now;
      }
      if (dateFrom.Value == null)
      {
        dateFrom.Value = DateTime.Today;
      }

    }

    protected void btnUpload_Click(object sender, EventArgs e)
    {
      lbStatus.InnerHtml = "";
      gvFileUpload.DataSource = null;
      gvFileUpload.DataBind();
      gvRate.DataSource = null; gvRate.DataBind();
      if (ulfInternalRate.HasFile)
      {
        hfc = Request.Files;
        lbStatus.InnerHtml = "";
        string ext = Path.GetExtension(hfc[0].FileName);
        if (ext.CompareTo(".xls") != 0)
        {
          lbStatus.InnerHtml = "Extension Internal rate not .xls ";
          lbStatus.Style.Add(HtmlTextWriterStyle.Color, "Red");
          return;
        }
        else
        {
          if (tblInternalRateService.GettblInternalRateOnDay().Count != 0)
          {
            pupMsg.ShowOnPageLoad = true;
          }
          else
          {
            gvFileUpload.DataSource = UploadFileRate(hfc, false);
            gvFileUpload.DataBind();
            gvFileUpload.Visible = true;
            pupMsg.ShowOnPageLoad = false;
          }
        }
      }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {

      if (dateFrom.Value != null && dateTo.Value != null)
      {
        try
        {
          gvFileUpload.DataSource = tblFileUploadService.GetRateFileByCreatedDate(DateTime.Parse(dateFrom.Value.ToString()), DateTime.Parse(dateTo.Value.ToString()), CurrentUser.Peoplewise);
          gvFileUpload.DataBind();
          gvFileUpload.Visible = true;
        }
        catch (Exception ex)
        {
          Logger.Error("WebRobot.Pages.ImportRatePage::btnSearch_Click", ex);
        }
      }
    }

    protected void gvFileUpload_PageIndexChanged(object sender, EventArgs e)
    {
      if (dateFrom.Value != null && dateTo.Value != null)
      {
        try
        {
          gvFileUpload.DataSource = tblFileUploadService.GetRateFileByCreatedDate(DateTime.Parse(dateFrom.Value.ToString()), DateTime.Parse(dateTo.Value.ToString()), CurrentUser.Peoplewise);
          gvFileUpload.DataBind();
          gvFileUpload.Visible = true;
        }
        catch (Exception ex)
        {
          Logger.Error("WebRobot.Pages.ImportRatePage::gvFileUpload_PageIndexChanged", ex);
        }
      }
    }

    protected void gvFileUpload_BeforeColumnSortingGrouping(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewBeforeColumnGroupingSortingEventArgs e)
    {
      if (dateFrom.Value != null && dateTo.Value != null)
      {
        try
        {
          gvFileUpload.DataSource = tblFileUploadService.GetRateFileByCreatedDate(DateTime.Parse(dateFrom.Value.ToString()), DateTime.Parse(dateTo.Value.ToString()), CurrentUser.Peoplewise);
          gvFileUpload.DataBind();
          gvFileUpload.Visible = true;
        }
        catch (Exception ex)
        {
          Logger.Error("WebRobot.Pages.ImportRatePage::gvFileUpload_BeforeColumnSortingGrouping", ex);
        }
      }
    }

    #region Event in popup

    protected void btnOk_Click(object sender, EventArgs e)
    {
      try
      {
        tblFileUploadCollection listFile = new tblFileUploadCollection();
        listFile = UploadFileRate(hfc, true);
        gvFileUpload.DataSource = listFile;
        gvFileUpload.DataBind();
        if (listFile.Count > 0)
        {

          gvFileUpload.Visible = true;
          pupMsg.ShowOnPageLoad = false;
        }
      }
      catch (Exception ex)
      {
        Logger.Error("WebRobot.Pages.ImportInternalRate::btnOk_Click", ex);
      }
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
      if (tblInternalRateService.GettblInternalRateOnDay().Count != 0)
      {
        pupMsg.ShowOnPageLoad = true;
      }
      else
        pupMsg.ShowOnPageLoad = false;
    }

    #endregion

    #endregion

    #region Helper

    private tblFileUploadCollection UploadFileRate(HttpFileCollection fileColection, bool reUpload)
    {
      pupMsg.ShowOnPageLoad = false;
      string myPath = "";
      tblFileUpload file = new tblFileUpload();

      // Get the HttpFileCollection
      HttpPostedFile sfile = fileColection[0];
      tblFileUploadCollection listFile = new tblFileUploadCollection();

      try
      {

        if (sfile.ContentLength > 0)
        {

          string ext = Path.GetExtension(sfile.FileName);

          file.UploadDate = DateTime.Today;

          if (reUpload)
          {
            file.FileName = sfile.FileName.Split('.')[0] + " " + DateTime.Now.ToString("yyyyMMddHHmmss") + "(reUp)" + ext;
          }
          else
          {
            file.FileName = sfile.FileName.Split('.')[0] + " " + DateTime.Now.ToString("yyyyMMddHHmmss") + ext;
          }
          file.UploadBy = CurrentUser.Peoplewise;

          myPath = Server.MapPath("~") + GetValueInXML("IntRate") + DateTime.Today.ToString("yyyyMMdd");

          if (!Directory.Exists(myPath))
          {
            Directory.CreateDirectory(myPath);
          }

          myPath = myPath + "\\" + Path.GetFileName(file.FileName);
          file.FileName = Path.GetFileName(file.FileName);
          sfile.SaveAs(myPath);
          file.Path = myPath;

          tblFileUploadService.CreatetblFileUpload(file);
          listFile.Add(file);
          InternalRate fileIntRate = new InternalRate();
          int rateUploaded = 0;
          rateUploaded = fileIntRate.ImportInternalRate(file.Path, CurrentUser.Peoplewise, file.UploadDate.Value);
          if (rateUploaded != 0)
          {
            lbStatus.InnerHtml = "Upload sucessful!";
            lbStatus.Style.Add(HtmlTextWriterStyle.Color, "Blue");
          }
          else
          {
            listFile.Remove(file);
            lbStatus.InnerHtml = "Upload error!";
            lbStatus.Style.Add(HtmlTextWriterStyle.Color, "Red");


            tblFileUpload f = new tblFileUpload();
            f = tblFileUploadService.GettblFileUploadByFileName(file.FileName, CurrentUser.Peoplewise);
            tblFileUploadService.DeletetblFileUpload(f.ID);
            File.Delete(f.Path);
          }
        }
        return listFile;
      }
      catch (Exception ex)
      {
        Logger.Error("WebRobot.Pages.ViewTransaction:UploadFileRate", ex);
        lbStatus.InnerHtml = "Upload error!";
        lbStatus.Style.Add(HtmlTextWriterStyle.Color, "Red");

        if (tblInternalRateService.GettblInternalRateOnDay().Count != 0)
        {
          pupMsg.ShowOnPageLoad = true;
        }
        else
          pupMsg.ShowOnPageLoad = false;
        return listFile;
      }
    }

    #endregion

  }
}
